import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '../components/login/login.vue'
import home from '../components/home/home.vue'
import welcome from '../components/home/welcome.vue'
import users from '../components/home/userlist/users.vue'
import rights from '../components/home/rights/rights.vue'
import roles from '../components/home/rights/roles.vue'
import goods from '../components/goods/goods.vue'
import params from '../components/goods/params.vue'
import categories from '../components/goods/categories.vue'
import addGoods from '../components/goods/add.vue'
import order from '../components/order/order.vue'
import report from '../components/report/report.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: login },
  {
    path: '/home',
    component: home,
    redirect: '/welcome',
    children: [{
      path: '/welcome', component: welcome
    },
    {
      path: '/users', component: users
    },
    {
      path: '/rights', component: rights
    },
    {
      path: '/roles', component: roles
    },
    {
      path: '/goods', component: goods
    },
    {
      path: '/params', component: params
    },
    {
      path: '/categories', component: categories
    },
    {
      path: '/goods/add', component: addGoods
    },
    {
      path: '/orders', component: order
    },
    {
      path: '/reports', component: report
    }
    ]
  }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})
export default router
